package model;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: a
 * Date: 2022-12-21
 * Time: 23:37
 */
public class MyArrayList {
    public MicroBlog[] elem;
    private int usedSize;
    private static final int DEFAULT_SIZE = 10;

    public MyArrayList(){
        this.elem = new MicroBlog[DEFAULT_SIZE];
    }

    public boolean isFull(){
        return this.usedSize >= this.elem.length;
    }

    public void add(MicroBlog microBlog){
        if(isFull()){
            MicroBlog[] t = new MicroBlog[2 * this.elem.length];
            for(int i = 0; i < this.elem.length; i++){
                t[i] = this.elem[i];
            }
            this.elem = t;
        }
        this.elem[this.usedSize] = microBlog;
        this.usedSize++;
    }

    public void swap(int i, int j){
        MicroBlog tmp = this.elem[i];
        this.elem[i] = this.elem[j];
        this.elem[j] = tmp;
    }

    public void quickSort(int l, int r){
        if(l >= r) return;
        int x = this.elem[l].getBlogId();
        int i = l - 1;
        int j = r + 1;
        while(i < j){
            do{
                i++;
            }while(this.elem[i].getBlogId() < x);

            do{
                j--;
            }while(this.elem[j].getBlogId() > x);

            if(i < j) swap(i, j);
        }
        quickSort(l, j);
        quickSort(j + 1, r);
    }

    public void sort(){
        int l = 0;
        int r = this.usedSize - 1;
        quickSort(l, r);
    }

    public int size(){
        return this.usedSize;
    }
}
